package com.gali.mall.common;

import com.gali.mall.exception.GaLiExceptionEnum;

/**
 * 统一返回结果集
 * @param <T>
 */
public class ResultInfo<T> {

    private Integer status;

    private String msg;

    private T data;

    private static final int OK_CODE = 10000;

    private static final String OK_MSG = "SUCCESS";

    public ResultInfo(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ResultInfo(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ResultInfo() {
        this(OK_CODE, OK_MSG);
    }

    public static <T> ResultInfo<T> success() {
        return new ResultInfo<>();
    }

    public static <T> ResultInfo<T> success(T result) {
        ResultInfo<T> response = new ResultInfo<>();
        response.setData(result);
        return response;
    }

    public static <T> ResultInfo<T> error(Integer code, String msg) {
        return new ResultInfo<>(code, msg);
    }

    public static <T> ResultInfo<T> error(GaLiExceptionEnum ex) {
        return new ResultInfo<>(ex.getCode(), ex.getMsg());
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultInfo{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
